package com.kesh.ltank.elements;

import android.graphics.Point;

import com.android.framework.Graphics;
import com.android.framework.Image;
import com.kesh.ltank.game.GlobalVars;
import com.kesh.ltank.game.GlobalVars.PlayImages;
import com.kesh.ltank.game.GlobalVars.PlayObjects;
import com.kesh.ltank.game.Utils;

public class Platform {
	
	private Graphics pltfrmGpic;
	private Point pltfrmStrt;
	private Point pltfrmLen;
	
	private Point playImageOrigLen;
	private Point playImageScaledLen;
	
	public Platform() {
		pltfrmStrt = GlobalVars.playFieldStart;
		pltfrmLen = GlobalVars.playFieldLen;
		playImageOrigLen = GlobalVars.playImageOrigLen;
		
		playImageScaledLen = new Point(pltfrmLen.x/GlobalVars.lvlFieldLen.x,pltfrmLen.y/GlobalVars.lvlFieldLen.y);
		GlobalVars.playImageScaledLen = playImageScaledLen;
	}

	public void paintPlatform(Graphics inPltfrmGpic, LTank lTank){
		
		pltfrmGpic = inPltfrmGpic;
		PlayObjects lvlBaseField[][] = lTank.getLvlBaseField();
		PlayObjects lvlPlayField[][] = lTank.getLvlPlayField();
		
		Point curPos = new Point(pltfrmStrt.x, pltfrmStrt.y);
		
		for (int i = 0; i < GlobalVars.lvlFieldLen.x; i++) {
			for (int j = 0; j < GlobalVars.lvlFieldLen.y; j++) {
				
				Image baseImage = Utils.getPlayImage(lvlBaseField[j][i].id).image;
				Image topImage = Utils.getPlayImage(lvlPlayField[j][i].id).image;
				if(playImageOrigLen!=playImageScaledLen){
					Point imgOrigStrtPoint = new Point(0,0);
					pltfrmGpic.drawScaledImage(PlayImages.Ground.image, curPos, playImageScaledLen, imgOrigStrtPoint, playImageOrigLen);
					pltfrmGpic.drawScaledImage(baseImage, curPos, playImageScaledLen, imgOrigStrtPoint, playImageOrigLen);
					pltfrmGpic.drawScaledImage(topImage, curPos, playImageScaledLen, imgOrigStrtPoint, playImageOrigLen);
					imgOrigStrtPoint=null;
					curPos.x = curPos.x + playImageScaledLen.x;
				} else {
					pltfrmGpic.drawImage(PlayImages.Ground.image, curPos);
					pltfrmGpic.drawImage(baseImage, curPos);
					pltfrmGpic.drawImage(topImage, curPos);
					curPos.x = curPos.x + playImageOrigLen.x;
				}
				baseImage = null;
				topImage = null;
			}
			
			if(playImageOrigLen!=playImageScaledLen){
				curPos.x = pltfrmStrt.x;
				curPos.y = curPos.y + playImageScaledLen.y;
			}else{
				curPos.x = pltfrmStrt.x;
				curPos.y = curPos.y + playImageOrigLen.y;
			}
		}
	}

}
